#include "inputAnalyze.h"

int commandAnalyze(char *input, cmd_t *command)
{

    char *cmdName[8] = {"cd", "ls", "pwd", "puts", "gets", "rm", "mkdir", "rmdir"};
    int validFlag = 0;

    char *cmd = strtok(input, " ");

    // strcat(cmd,"\0");
    for (int i = 0; i < 8; ++i)
    {

        int ret = strcmp(cmdName[i], cmd);
        if (ret == 0)
        {
            command->cmdType = i;
            validFlag = 1;
            char *parameter1 = strtok(NULL, " ");

            if (parameter1 != NULL)
            {
                if (command->cmdType == ISPWD || command->cmdType == ISLS)
                {
                    printCmdResult(cmd);
                    return 1;
                }

                memcpy(command->parameter1, parameter1, strlen(parameter1));

                char *parameter2 = strtok(NULL, " ");

                if (parameter2 != NULL)
                {
                    if (command->cmdType == ISCD || command->cmdType == ISRM || command->cmdType == ISMKDIR || command->cmdType == ISRMDIR)
                    {
                        printCmdResult(cmd);
                        return 1;
                    }
                    memcpy(command->parameter2, parameter2, strlen(parameter2));
                }
            }
            break;
        }
    }
    printf("%d\n", command->cmdType);
    printf("%s\n", command->parameter1);
    printf("%s\n", command->parameter2);
    if (validFlag == 0)
    {
        printf("ERROR : command %s is not found!\n", cmd);
        return 1;
    }

    char *waste = strtok(NULL, " ");
    if (waste != NULL)
    {
        printCmdResult(cmd);
        return 1;
    }

    return 0;
}
